using Microsoft.EntityFrameworkCore;
using S.M.SmartMedical.Domain.Entities;
using S.M.SmartMedical.Domain.Enums;

namespace S.M.SmartMedical.Infrastructure
{
    /// <summary>
    /// 数据库上下文
    /// </summary>
    public class MyDbContext : DbContext
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MyDbContext(DbContextOptions options) : base(options)
        {
        }

        /// <summary>
        /// 用户表
        /// </summary>
        public DbSet<User> Users { get; set; }

        /// <summary>
        /// 医院院区表
        /// </summary>
        public DbSet<HospitalArea> HospitalAreas { get; set; }

        /// <summary>
        /// 医疗检查项目表
        /// </summary>
        public DbSet<MedicalProject> MedicalProjects { get; set; }

        /// <summary>
        /// 开单申请表
        /// </summary>
        public DbSet<OrderApplication> OrderApplications { get; set; }

        /// <summary>
        /// 开单申请项目关联表
        /// </summary>
        public DbSet<OrderApplicationItem> OrderApplicationItems { get; set; }

        /// <summary>
        /// 轮播图表
        /// </summary>
        public DbSet<Carousel> Carousels { get; set; }

        /// <summary>
        /// 医院表
        /// </summary>
        public DbSet<Hospital> Hospitals { get; set; }

        /// <summary>
        /// 模型配置
        /// </summary>
        /// <param name="modelBuilder">模型构建器</param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置User实体
            modelBuilder.Entity<User>(entity =>
            {
                entity.ToTable("Users");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.UserName).IsRequired().HasMaxLength(50);
                entity.Property(e => e.RealName).IsRequired().HasMaxLength(50);
                entity.Property(e => e.Phone).HasMaxLength(20);
                entity.Property(e => e.Email).HasMaxLength(100);
                entity.Property(e => e.Password).IsRequired().HasMaxLength(255);
                entity.Property(e => e.Status).IsRequired();
                entity.Property(e => e.CreateTime).IsRequired();
                entity.Property(e => e.CreateUserName).HasMaxLength(50);
                entity.Property(e => e.UpdateUserName).HasMaxLength(50);
                entity.Property(e => e.IsDeleted).IsRequired();

                // 创建索引
                entity.HasIndex(e => e.UserName).IsUnique();
                entity.HasIndex(e => e.Phone);
                entity.HasIndex(e => e.Email);
            });

            // 配置HospitalArea实体
            modelBuilder.Entity<HospitalArea>(entity =>
            {
                entity.ToTable("HospitalAreas");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Address).IsRequired().HasMaxLength(200);
                entity.Property(e => e.Phone).HasMaxLength(20);
                entity.Property(e => e.Description).HasMaxLength(500);
                entity.Property(e => e.IsSelfServiceEnabled).IsRequired();
                entity.Property(e => e.Tags).HasMaxLength(500);
                entity.Property(e => e.Longitude).HasColumnType("decimal(10,7)");
                entity.Property(e => e.Latitude).HasColumnType("decimal(10,7)");
                entity.Property(e => e.SortOrder).IsRequired();
                entity.Property(e => e.IsEnabled).IsRequired();
                entity.Property(e => e.CreateTime).IsRequired();
                entity.Property(e => e.CreateUserName).HasMaxLength(50);
                entity.Property(e => e.UpdateUserName).HasMaxLength(50);
                entity.Property(e => e.IsDeleted).IsRequired();

                // 创建索引
                entity.HasIndex(e => e.Name);
                entity.HasIndex(e => e.IsSelfServiceEnabled);
                entity.HasIndex(e => e.IsEnabled);
                entity.HasIndex(e => e.SortOrder);
            });

            // 配置医疗检查项目表
            modelBuilder.Entity<MedicalProject>(entity =>
            {
                entity.ToTable("MedicalProjects");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Description).HasMaxLength(500);
                entity.Property(e => e.Price).IsRequired().HasColumnType("decimal(10,2)");
                entity.Property(e => e.Category).IsRequired();
                entity.Property(e => e.Code).IsRequired().HasMaxLength(50);
                entity.Property(e => e.IsEnabled).IsRequired();
                entity.Property(e => e.SortOrder).IsRequired();
                entity.Property(e => e.Tags).HasMaxLength(500);
                entity.Property(e => e.PreparationInstructions).HasMaxLength(1000);
                entity.Property(e => e.CreateTime).IsRequired();
                entity.Property(e => e.CreateUserName).HasMaxLength(50);
                entity.Property(e => e.UpdateUserName).HasMaxLength(50);
                entity.Property(e => e.IsDeleted).IsRequired();

                // 外键关系
                entity.HasOne(e => e.HospitalArea)
                      .WithMany()
                      .HasForeignKey(e => e.HospitalAreaId)
                      .OnDelete(DeleteBehavior.Restrict);

                // 创建索引
                entity.HasIndex(e => e.Name);
                entity.HasIndex(e => e.Code).IsUnique();
                entity.HasIndex(e => e.Category);
                entity.HasIndex(e => e.HospitalAreaId);
                entity.HasIndex(e => e.IsEnabled);
                entity.HasIndex(e => e.SortOrder);
            });

            // 配置开单申请表
            modelBuilder.Entity<OrderApplication>(entity =>
            {
                entity.ToTable("OrderApplications");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.ApplicationNumber).IsRequired().HasMaxLength(50);
                entity.Property(e => e.Status).IsRequired();
                entity.Property(e => e.ProjectCount).IsRequired();
                entity.Property(e => e.TotalAmount).IsRequired().HasColumnType("decimal(10,2)");
                entity.Property(e => e.Remarks).HasMaxLength(500);
                entity.Property(e => e.AppointmentTimeSlot).HasMaxLength(20);
                entity.Property(e => e.ContactPhone).HasMaxLength(20);
                entity.Property(e => e.ReviewerName).HasMaxLength(50);
                entity.Property(e => e.ReviewComments).HasMaxLength(500);
                entity.Property(e => e.PaymentMethod).HasMaxLength(50);
                entity.Property(e => e.CreateTime).IsRequired();
                entity.Property(e => e.CreateUserName).HasMaxLength(50);
                entity.Property(e => e.UpdateUserName).HasMaxLength(50);
                entity.Property(e => e.IsDeleted).IsRequired();

                // 外键关系
                entity.HasOne(e => e.User)
                      .WithMany()
                      .HasForeignKey(e => e.UserId)
                      .OnDelete(DeleteBehavior.Restrict);

                entity.HasOne(e => e.HospitalArea)
                      .WithMany()
                      .HasForeignKey(e => e.HospitalAreaId)
                      .OnDelete(DeleteBehavior.Restrict);

                // 创建索引
                entity.HasIndex(e => e.ApplicationNumber).IsUnique();
                entity.HasIndex(e => e.UserId);
                entity.HasIndex(e => e.HospitalAreaId);
                entity.HasIndex(e => e.Status);
                entity.HasIndex(e => e.CreateTime);
            });

            // 配置轮播图实体
            modelBuilder.Entity<Carousel>(entity =>
            {
                entity.ToTable("Carousels");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Title).IsRequired().HasMaxLength(100);
                entity.Property(e => e.ImageUrl).IsRequired().HasMaxLength(500);
                entity.Property(e => e.LinkUrl).HasMaxLength(500);
                entity.Property(e => e.Status).IsRequired();
                entity.Property(e => e.Uploader).HasMaxLength(50);
                entity.Property(e => e.UploadTime).IsRequired();
                entity.Property(e => e.SortOrder).IsRequired();
                entity.Property(e => e.EffectiveStartTime);
                entity.Property(e => e.EffectiveEndTime);
                entity.Property(e => e.CreateTime).IsRequired();
                entity.Property(e => e.CreateUserName).HasMaxLength(50);
                entity.Property(e => e.UpdateUserName).HasMaxLength(50);
                entity.Property(e => e.IsDeleted).IsRequired();

                // 创建索引
            entity.HasIndex(e => e.Title);
            entity.HasIndex(e => e.Status);

            // 配置Hospital实体
            modelBuilder.Entity<Hospital>(entity =>
            {
                entity.ToTable("Hospitals");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
                entity.Property(e => e.ImageUrl).HasMaxLength(500);
                entity.Property(e => e.Level).IsRequired().HasMaxLength(20);
                entity.Property(e => e.Type).IsRequired().HasMaxLength(20);
                entity.Property(e => e.Address).IsRequired().HasMaxLength(200);
                entity.Property(e => e.Phone).IsRequired().HasMaxLength(50);
                entity.Property(e => e.Description).HasMaxLength(1000);
                entity.Property(e => e.IsEnabled).IsRequired();
                entity.Property(e => e.Uploader).HasMaxLength(50);
                entity.Property(e => e.UploadTime).IsRequired();
                entity.Property(e => e.CreateTime).IsRequired();
                entity.Property(e => e.UpdateTime).IsRequired();
                entity.Property(e => e.IsDeleted).IsRequired();

                // 创建索引
                entity.HasIndex(e => e.Name);
                entity.HasIndex(e => e.Level);
                entity.HasIndex(e => e.Type);
                entity.HasIndex(e => e.IsEnabled);
                entity.HasIndex(e => e.CreateTime);
            });
                entity.HasIndex(e => e.SortOrder);
                entity.HasIndex(e => e.UploadTime);
            });

            // 配置开单申请项目关联表
            modelBuilder.Entity<OrderApplicationItem>(entity =>
            {
                entity.ToTable("OrderApplicationItems");
                entity.HasKey(e => e.Id);
                entity.Property(e => e.ProjectName).IsRequired().HasMaxLength(100);
                entity.Property(e => e.ProjectPrice).IsRequired().HasColumnType("decimal(10,2)");
                entity.Property(e => e.ProjectDescription).HasMaxLength(500);
                entity.Property(e => e.Quantity).IsRequired();
                entity.Property(e => e.SubTotal).IsRequired().HasColumnType("decimal(10,2)");
                entity.Property(e => e.StatusRemarks).HasMaxLength(200);
                entity.Property(e => e.ResultFilePath).HasMaxLength(500);
                entity.Property(e => e.CreateTime).IsRequired();
                entity.Property(e => e.CreateUserName).HasMaxLength(50);
                entity.Property(e => e.UpdateUserName).HasMaxLength(50);
                entity.Property(e => e.IsDeleted).IsRequired();

                // 外键关系
                entity.HasOne(e => e.OrderApplication)
                      .WithMany(o => o.Items)
                      .HasForeignKey(e => e.OrderApplicationId)
                      .OnDelete(DeleteBehavior.Cascade);

                entity.HasOne(e => e.MedicalProject)
                      .WithMany()
                      .HasForeignKey(e => e.MedicalProjectId)
                      .OnDelete(DeleteBehavior.Restrict);

                // 创建索引
                entity.HasIndex(e => e.OrderApplicationId);
                entity.HasIndex(e => e.MedicalProjectId);
            });
        }
    }
}
